<?php 
# For setup Time_zone
date_default_timezone_set('Asia/Shanghai');

#Finding the Current Date and Time

print date('r');
echo "<br/>";

$when = new DateTime();
print $when->format('r');
echo "<br/>";

#Finding time parts
$now_1 = getdate();
$now_2 = localtime();
print "{$now_1['hours']}:{$now_1['minutes']}:{$now_1['seconds']}\n"; 
echo "<br/>";
print "{$now_1['month']}:{$now_1['mday']}:{$now_1['year']}\n"; 
echo "<br/>";
print "$now_2[2]:$now_2[1]:$now_2[0]";
echo "<br/>";

#Printing a Date or Time in a Specified Format
print date('Y/M/d');
echo "<br/>";
$when = new DateTime();
print $when->format('d/M/Y');
echo "<br/>";
#print date('DATE_W3C');

#Finding the Difference of Two Dates
// 7:32:56 pm on May 10, 1965
$first = new DateTime("1965-05-10 7:32:56pm", new DateTimeZone('America/New_York'));
// 4:29:11 am on November 20, 1962
$second = new DateTime("1962-11-20 4:29:11am", new DateTimeZone('America/New_York'));
$diff = $second->diff($first);
printf("The two dates have %d weeks, %s days, " .
       "%d hours, %d minutes, and %d seconds " .
       "elapsed between them.",
       floor($diff->format('%a') / 7),
       $diff->format('%a') % 7,
       $diff->format('%h'),
       $diff->format('%i'),
       $diff->format('%s'));
echo "<br/>";
    
#3.6 Finding the Day in a Week, Month, or Year
print "Today is day " . date('d') . ' of the month and ' . date('z') . ' of the year.';
echo "<br/>";
$birthday = new DateTime('January 17, 1706', new DateTimeZone('America/New_York'));
print "Benjamin Franklin was born on a " . $birthday->format('l') . ", " . "day " . $birthday->format('N') . " of the week.";
echo "<br/>";

#3.7 Validating a Date
// $ok is true - March 10, 1993 is a valid date
$ok = checkdate(3, 10, 1993);
echo "<br/>";
// $not_ok is false - February 30, 1962 is not a valid date 
$not_ok = checkdate(2, 30, 1962);
echo "<br/>";


#3.10 Calculating Time with Time Zones and Daylight Saving Time
$nowInNewYork = new DateTime('now', new DateTimeZone('America/New_York')); 
$nowInCalifornia = new DateTime('now', new DateTimeZone('America/Los_Angeles'));
$nowInShanghai = new DateTime('now', new DateTimeZone('Asia/Shanghai'));
printf("It's %s in New York but %s in California and %s in China.",
        $nowInNewYork->format(DateTime::RFC850),
        $nowInShanghai->format(DateTime::RFC850),
        $nowInCalifornia->format(DateTime::RFC850));
echo "<br/>";
?>
